package com.naiterui.ehp.bs.b2c.product.service.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bs.b2c.product.entity.SkuImage;
import com.naiterui.ehp.bs.b2c.product.repository.ISkuImageRepository;
import com.naiterui.ehp.bs.b2c.product.service.ISkuImageService;

@Service
public class SkuImageServiceImpl implements ISkuImageService {

    /**
     * sku图片url前缀
     */
    private static final String IMG_URL_PRE_SKU = ConfigUtil.getString("img.url.pre.sku");

    @Autowired
    private ISkuImageRepository skuImageRepository;

    @Override
    public String getSkuIcon(Long skuId) {
        SkuImage skuImage = this.skuImageRepository.getBySkuIdAndType(skuId, SkuImage.TYPE_ICON);
        return Optional.ofNullable(skuImage).map(skuImageMap -> IMG_URL_PRE_SKU + skuImage.getPath()).orElse(null);
    }

    @Override
    public Map<String, String> imgMap(Long skuId) {
        List<SkuImage> bigSkuImage = this.skuImageRepository.findBySkuIdAndTypeOrderByIdAsc(skuId, SkuImage.TYPE_BIG);
        List<SkuImage> thumbSkuImage = this.skuImageRepository.findBySkuIdAndTypeOrderByIdAsc(skuId, SkuImage.TYPE_THUMB);
        LinkedHashMap<String, String> linkedHashMap = Maps.newLinkedHashMap();
        for (int i = 0, thumbSkuImageSize = thumbSkuImage.size(); i < thumbSkuImageSize; i++) {
            SkuImage skuImage = thumbSkuImage.get(i);
            linkedHashMap.put(skuImage.getPath(), bigSkuImage.get(i).getPath());
        }
        return linkedHashMap;
    }

}
